import {EventEmitter} from "events";
import { InviteModeEnums, JoinModeEnums, ChatModeEnums, RoomTypeEnums } from './enums'


/**
 * 媒体约束
 */
export type MediaOptions = {
  url?: string;
  // 静音
  mute?: true;
  // 音量
  volume?: true;
  mediaConstraints?: {
    video: true;
    audio: true;
  };
  // boolean || {}
  dataChannel?: {};
}

export type MemberBase = {
  socketId: string;
  userId: string;
  username: string;
}

export interface MemberOption extends MemberBase {
  socketId: string;
  classId?: string;
  classname?: string;
}

export class MemberInterface extends EventEmitter {
  readonly userId: string;
  readonly username: string;
  readonly socketId: string;
  constructor(option) {
    super();
    this.userId = option.userId;
    this.username = option.username;
    this.socketId = option.socketId;
  }

  json (): MemberBase {
    const { userId, username, socketId } = this;
    return { userId, username, socketId }
  }
}

export type RoomBase = {
  creator: MemberBase;
  classType: RoomTypeEnums;
  classId: string;
  classname?: string;
  inviteMode?: InviteModeEnums;
  joinMode?: JoinModeEnums;
  chatMode?: ChatModeEnums;
}

export class RoomInterface extends EventEmitter {
  readonly creator: MemberBase
  readonly classId: string;
  readonly classname?: string;
  readonly classType: RoomTypeEnums = RoomTypeEnums.RESIDENT;
  readonly inviteMode: InviteModeEnums = InviteModeEnums.NEED_VERIFY;
  readonly joinMode: JoinModeEnums = JoinModeEnums.NEED_VERIFY;
  readonly chatMode: ChatModeEnums = ChatModeEnums.GROUP;

  constructor(option) {
    super();
    this.creator = option.creator;
    this.classId = option.classId;
    this.classname = option.classname;
    this.classType = option.classType;
    this.inviteMode = option.inviteMode;
    this.joinMode = option.joinMode;
    this.chatMode = option.chatMode;
  }

  createRoom (option: RoomBase) {
    const { creator, classId, classname, classType, inviteMode, joinMode, chatMode } = option;
    return new RoomInterface({
      creator, classId, classname, classType, inviteMode, joinMode, chatMode
    })
  }

  json (): RoomBase {
    const { creator, classType, classId, classname, chatMode } = this;
    return { creator, classType, classId, classname, chatMode }
  }
}


